package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Adapter.FaseBindAdapter;
import liumengbaio.lolzhangyoubao.Activity.Adapter.ServernameAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.BindInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.BindRoleInfo;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/5.
 */
public class BindRoleActivity extends AppCompatActivity {
    private String[] server_name, server_id;
    private List<String> list = new ArrayList<>();
    private String player_name, parmas_id, bind_name, fast_bind_area_name;
    private Button btn_bind, edit_head_left_title;
    private EditText editText_name;
    private BindInfo bindInfo = new BindInfo();
    private ListView area_listview, fase_bind_listview;
    private TextView tv_bind, tv_bind_listview;
    private SharedPreferences sp;
    private SharedPreferences.Editor edit;
    private BindRoleInfo bindRoleInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bind_role_activity);
        sp = getSharedPreferences("bindInfo", MODE_PRIVATE);
        edit = sp.edit();
        initView();
        initData();
    }

    private void initView() {
        editText_name = (EditText) findViewById(R.id.player_name);
        tv_bind = (TextView) findViewById(R.id.tv_bind);
        btn_bind = (Button) findViewById(R.id.btn_bind);
        edit_head_left_title = (Button) findViewById(R.id.edit_head_left_title);
        area_listview = (ListView) findViewById(R.id.area_listview);
        fase_bind_listview = (ListView) findViewById(R.id.fase_bind_listview);
        tv_bind_listview = (TextView) findViewById(R.id.tv_bind_listview);
    }

    private void initData() {
        server_name = new String[]{"艾欧尼亚   电信一", "祖安   电信二", "诺克萨斯   电信三", "班德尔城   电信四", "皮尔特沃夫   电信五"
                , "战争学院   电信六", "巨神峰   电信七", "雷瑟守备   电信八", "裁决之地   电信九", "黑色玫瑰   电信十", "暗影岛   电信十一", "钢铁烈阳   电信十二"
                , "均衡教派   电信十三", "水晶之痕   电信十四", "影流   电信十五", "守望之海   电信十六", "征服之海   电信十七", "卡拉曼达   电信十八"
                , "皮城警备   电信十九", "比尔吉沃特   网通一", "德玛西亚   网通二", "弗雷尔卓德   网通三", "无畏先锋   网通四", "恕瑞玛   网通五"
                , "扭曲丛林   网通六", "巨龙之巢   网通七", "教育网专区   教育一"};
        server_id = new String[]{"1", "3", "4", "5", "7", "8", "10", "11", "13", "14", "15", "17"
                , "18", "19", "22", "23", "24", "25"
                , "27", "2", "6", "9", "12", "16"
                , "20", "26", "21"};
        edit_head_left_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        area_listview.setAdapter(new ServernameAdapter(server_name, BindRoleActivity.this));
        tv_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                area_listview.setVisibility(View.VISIBLE);
            }
        });
        area_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_bind.setText(server_name[position]);
                parmas_id = server_id[position];
                bind_name = server_name[position].substring(0, 5).trim();
                Log.i("001", "" + bind_name);
                area_listview.setVisibility(View.GONE);
            }
        });
        btn_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player_name = editText_name.getText().toString();
                String url = "http://lol.magnet.zhangyoubao.com/service/" +
                        "rest?api=search.user&game=lol&platformVersion=400901&modle_=vivo+Y15T";
                String value = "apiVersion=v1&platform=android&os=android&osVersion=17&secre" +
                        "tSignature=TeTAhCJ6n3lZXI5uqOcVfIwWJHk%3D&game=lol&params%5Breques" +
                        "t%5D=%7B%22area%22%3A" + parmas_id + "%2C%22name%22%3A%22" + player_name + "%" +
                        "22%7D&sign=&platformVersion=400901&time=1473057016&secretId=AKIDz8" +
                        "krbsJ5yKBZQpn74WFkmLPx3gnPhESA&nonce=560867&userToken=1285c08b6124" +
                        "11d0bfc35f213eb5d92a73&secretVersion=v1.0&params%5Bver%5D=27&userId=" +
                        "15863305&api=search.user&deviceId=UcDAihrd4WMeNwR%2BvF4c%2BjtL6gm77or" +
                        "VBqYdtvp2Sis%3D&";

                try {
                    HttpUtils.doPostAsyn(url, value, new HttpUtils.CallBack() {
                        @Override
                        public void onRequestComplete(String result) {
                            Gson gson = new Gson();
                            bindInfo = gson.fromJson(result, BindInfo.class);
                            if (bindInfo.getData().getData().size() >= 0) {
                                bindRoleInfo = new BindRoleInfo();
                                bindRoleInfo.setName(player_name);
                                bindRoleInfo.setArea(parmas_id);
                                EventBus.getDefault().post(bindRoleInfo);
                            }

                        }
                    }, handler);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fast_bind_area_name = bind_name + "  " + player_name;
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Toast.makeText(BindRoleActivity.this, "没有找到召唤师", Toast.LENGTH_SHORT).show();
            }
            if (msg.what == 0) {
                if (list.size() < 3) {
                    if (list.contains(fast_bind_area_name))
                        return;
                    else
                        list.add(fast_bind_area_name);

                } else {
                    if (list.contains(fast_bind_area_name)) {
                        return;
                    } else {
                        list.remove(0);
                        list.add(fast_bind_area_name);
                    }
                }

                fase_bind_listview.setAdapter(new FaseBindAdapter(list, BindRoleActivity.this));
                for (int i = 0; i < list.size(); i++) {
                    edit.putString("list" + i, list.get(i));
                    Log.i("list", "list" + i + "," + list.get(i));
                }
                edit.commit();
                tv_bind_listview.setVisibility(View.VISIBLE);
                fase_bind_listview.setVisibility(View.VISIBLE);
            }
        }
    };

}
